package util;

import pojo.UserDO;

import javax.swing.*;
import java.awt.*;
import java.sql.*;
import java.util.ArrayList;

public class Util {

    public static Image getImage(String path) {
        return new ImageIcon(Util.class.getResource(path)).getImage();
    }

    public static ImageIcon getImageIcon(String path) {
        return new ImageIcon(Util.class.getResource(path));
    }


    private  Connection con;
    private  PreparedStatement preparedStatement;


    /**
     * 创建连接
     *
     * @return
     */
    public Connection getConnection() {
        try {
            // 1. 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        // 2. 创建连接
        try {
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/j189?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
                    "root", "231725");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
//        System.out.println("con:" + con);
        return con;
    }


    /**
     * 更新数据
     *
     * @throws SQLException
     */
    public void insert(String sql) {
        //1.创建连接
        con = getConnection();
        Statement statement = null;
        try {
            //2.获取执行SQL语句对象
            statement = con.createStatement();
            //3.执行sql语句
            int row = statement.executeUpdate(sql);
//            System.out.println("影响的行数：" + row);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //4.关闭连接
            close(con, statement);
        }
    }

    /**
     * 关闭连接
     *
     * @param con
     * @param statement
     */
    public void close(Connection con, Statement statement) {
        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    /**
     * 关闭连接 重载
     *
     * @param con
     * @param statement
     * @param rs
     */
    public void close(Connection con, Statement statement, ResultSet rs) {
        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

    }

//    public static void close() {
//        if (con != null) {
//            try {
//                con.close();
//            } catch (SQLException throwables) {
//                throwables.printStackTrace();
//            }
//        }
//
//        if (preparedStatement != null) {
//            try {
//                preparedStatement.close();
//            } catch (SQLException throwables) {
//                throwables.printStackTrace();
//            }
//        }
//
//    }

    /**
     * 查询
     */
    public ArrayList query() {
        Connection con = getConnection();
        Statement statement = null;
        ResultSet rs = null;
        ArrayList<UserDO> userlist = new ArrayList();
        try {
            //2.获取执行SQL语句的对象
            statement = con.createStatement();
            String sql = "select u_name,u_psw,u_money from t_user";
            //3.执行SQL并返回结果集
            rs = statement.executeQuery(sql);
            while (rs.next()) {
                UserDO user = new UserDO();
                user.setUsername(rs.getString(1));
                user.setPassword(rs.getString(2));
                user.setMoney(rs.getDouble(3));
                userlist.add(user);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //4.关闭
            close(con, statement, rs);
        }
        return userlist;
    }

    /**
     * 根据用户名和密码查询用户对象
     *
     * @param username
     * @param psw
     * @return
     */
    public UserDO getByUsernameAndPsw(String username, String psw) {
        Connection con = getConnection();
        PreparedStatement statement = null;
        ResultSet rs = null;
        UserDO user = null;
        String sql = "select * from t_user where u_name=? and u_psw=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setString(1, username);
            statement.setObject(2, psw);
            rs = statement.executeQuery();
            if (rs.next()) {
                user = new UserDO();
                user.setId(rs.getInt("u_id"));
                user.setUsername(rs.getString("u_name"));
                user.setPassword(rs.getString("u_psw"));
                user.setMoney(rs.getDouble("u_money"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, rs);
        }
        return user;
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public UserDO getById(int id) {
        Connection con = getConnection();
        PreparedStatement statement = null;
        ResultSet rs = null;
        UserDO user = null;
        String sql = "select * from t_user where u_id=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, id);
            rs = statement.executeQuery();
            if (rs.next()) {
                user = new UserDO();
                user.setId(rs.getInt("u_id"));
                user.setUsername(rs.getString("u_name"));
                user.setPassword(rs.getString("u_psw"));
                user.setMoney(rs.getDouble("u_money"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, rs);
        }
        return user;
    }

    /**
     * 根据用户名查询用户对象
     *
     * @param username
     * @return
     */
    public UserDO getByUsername(String username) {
        Connection con = getConnection();
        PreparedStatement statement = null;
        ResultSet rs = null;
        UserDO user = null;
        String sql = "select * from t_user where u_name=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, username);
            rs = statement.executeQuery();
            if (rs.next()) {
                user = new UserDO();
                user.setId(rs.getInt("u_id"));
                user.setUsername(rs.getString("u_name"));
                user.setPassword(rs.getString("u_psw"));
                user.setMoney(rs.getDouble("u_money"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, rs);
        }
        return user;
    }

    /**
     * 根据User对象中的数据进行修改
     *
     * @param user
     */
    public void update(UserDO user) {
        // 1. 创建连接
        Connection con = getConnection();
        PreparedStatement preparedStatement = null;
        String sql = "update t_user set u_psw=?,u_money=? where u_id=?";

        try {
            // 2. 获取执行SQL语句对象
            preparedStatement = con.prepareStatement(sql);
            preparedStatement.setObject(1, user.getPassword());
            preparedStatement.setObject(2, user.getMoney());
            preparedStatement.setObject(3, user.getId());

            // 3. 执行SQL语句
            int row = preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 4. 关闭连接
            close(con, preparedStatement);
        }
    }

//    public void setAutoCommit(){
//        try {
//            con.setAutoCommit(false);
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        }
//    }
//
//    public void commit(){
//        try {
//            con.commit();
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        }
//    }
//
//    public void rollback(){
//        try {
//            con.rollback();
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        }
//    }

    //用事务提交
    public void testTransition(UserDO loginUser, UserDO otherUser) {
        Connection con = getConnection();
        PreparedStatement preparedStatement = null;
        String sql = "update t_user set u_money=? where u_name=?";
        try {
            con.setAutoCommit(false);
            preparedStatement = con.prepareStatement(sql);
            preparedStatement.setObject(1, loginUser.getMoney());
            preparedStatement.setObject(2, loginUser.getUsername());
            preparedStatement.executeUpdate();

            preparedStatement = con.prepareStatement(sql);
            preparedStatement.setObject(1, otherUser.getMoney());
            preparedStatement.setObject(2, otherUser.getUsername());
            preparedStatement.executeUpdate();

            con.commit();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } finally {
            close(con, preparedStatement);
        }
    }

}